home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / hal.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  15.7 KB  |  286 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import string
  6. import posixpath
  7. from checkbox.lib.cache import cache
  8. from checkbox.lib.dmi import DmiNotAvailable
  9. from checkbox.lib.pci import Pci
  10. from checkbox.lib.usb import Usb
  11. from checkbox.properties import String
  12. from checkbox.registry import Registry
  13. from checkbox.registries.command import CommandRegistry
  14. from checkbox.registries.link import LinkRegistry
  15. from checkbox.registries.map import MapRegistry
  16.  
  17. class UnknownName(object):
  18.     
  19.     def __init__(self, function):
  20.         self._function = function
  21.  
  22.     
  23.     def __get__(self, instance, cls = None):
  24.         self._instance = instance
  25.         return self
  26.  
  27.     
  28.     def __call__(self, *args, **kwargs):
  29.         name = self._function(self._instance, *args, **kwargs)
  30.         if name and name.startswith('Unknown ('):
  31.             name = None
  32.         
  33.         return name
  34.  
  35.  
  36.  
  37. class DeviceRegistry(Registry):
  38.     '''Registry for HAL device information.
  39.  
  40.     Each item contained in this registry consists of the properties of
  41.     the corresponding HAL device.
  42.     '''
  43.     
  44.     def __init__(self, properties):
  45.         self._properties = properties
  46.  
  47.     
  48.     def __str__(self):
  49.         strings = _[1]
  50.         return '\n'.join(strings)
  51.  
  52.     
  53.     def _get_bus(self):
  54.         return self._properties.get('linux.subsystem')
  55.  
  56.     
  57.     def _get_category(self):
  58.         if 'system.hardware.vendor' in self._properties:
  59.             return 'SYSTEM'
  60.         if 'net.interface' in self._properties:
  61.             return 'NETWORK'
  62.         if 'pci.device_class' in self._properties:
  63.             class_id = self._properties['pci.device_class']
  64.             subclass_id = self._properties['pci.device_subclass']
  65.             if class_id == Pci.BASE_CLASS_NETWORK:
  66.                 return 'NETWORK'
  67.             if class_id == Pci.BASE_CLASS_DISPLAY:
  68.                 return 'VIDEO'
  69.             if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_USB:
  70.                 return 'USB'
  71.             if class_id == Pci.BASE_CLASS_COMMUNICATION and subclass_id == Pci.CLASS_COMMUNICATION_MODEM:
  72.                 return 'MODEM'
  73.             if class_id == Pci.BASE_CLASS_INPUT and subclass_id == Pci.CLASS_INPUT_SCANNER:
  74.                 return 'SCANNER'
  75.             if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_FIREWIRE:
  76.                 return 'FIREWIRE'
  77.         if self._get_product_id():
  78.             return 'OTHER'
  79.  
  80.     
  81.     def _get_driver(self):
  82.         return self._properties.get('info.linux.driver')
  83.  
  84.     
  85.     def _get_path(self):
  86.         return self._properties.get('linux.sysfs_path', '').replace('/sys', '')
  87.  
  88.     
  89.     def _get_product_id(self):
  90.         if 'info.subsystem' in self._properties:
  91.             product_id = '%s.product_id' % self._properties['info.subsystem']
  92.             if product_id in self._properties:
  93.                 return self._properties[product_id]
  94.         
  95.         if 'pnp.id' in self._properties:
  96.             match = re.match('^(?P<vendor_name>.*)(?P<product_id>[%s]{4})$' % string.hexdigits, self._properties['pnp.id'])
  97.             if match:
  98.                 return int(match.group('product_id'), 16)
  99.         
  100.  
  101.     
  102.     def _get_vendor_id(self):
  103.         if 'info.subsystem' in self._properties:
  104.             vendor_id = '%s.vendor_id' % self._properties['info.subsystem']
  105.             if vendor_id in self._properties:
  106.                 return self._properties[vendor_id]
  107.         
  108.  
  109.     
  110.     def _get_subproduct_id(self):
  111.         return self._properties.get('pci.subsys_product_id')
  112.  
  113.     
  114.     def _get_subvendor_id(self):
  115.         return self._properties.get('pci.subsys_vendor_id')
  116.  
  117.     
  118.     def _get_product(self):
  119.         bus = self._get_bus()
  120.         if bus in ('drm', 'net', 'platform', 'pci', 'pnp', 'scsi_generic', 'scsi_host', 'tty', 'usb', 'video4linux'):
  121.             return None
  122.         if 'usb.interface.number' in self._properties:
  123.             return None
  124.         if self._properties.get('info.category') == 'ac_adapter':
  125.             return None
  126.         for property in ('alsa.device_id', 'alsa.card_id', 'sound.card_id', 'battery.model', 'ieee1394.product', 'killswitch.name', 'oss.device_id', 'scsi.model', 'system.hardware.product', 'info.product'):
  127.             if property in self._properties:
  128.                 return self._properties[property]
  129.         
  130.  
  131.     _get_product = UnknownName(_get_product)
  132.     
  133.     def _get_vendor(self):
  134.         bus = self._get_bus()
  135.         if bus in ('drm', 'pci', 'rfkill', 'usb'):
  136.             return None
  137.         for property in ('battery.vendor', 'ieee1394.vendor', 'scsi.vendor', 'system.hardware.vendor', 'info.vendor'):
  138.             if property in self._properties:
  139.                 return self._properties[property]
  140.         
  141.  
  142.     _get_vendor = UnknownName(_get_vendor)
  143.     
  144.     def items(self):
  145.         return (('path', self._get_path()), ('bus', self._get_bus()), ('category', self._get_category()), ('driver', self._get_driver()), ('product_id', self._get_product_id()), ('vendor_id', self._get_vendor_id()), ('subproduct_id', self._get_subproduct_id()), ('subvendor_id', self._get_subvendor_id()), ('product', self._get_product()), ('vendor', self._get_vendor()), ('properties', MapRegistry(self._properties)), ('device', LinkRegistry(self)))
  146.  
  147.  
  148.  
  149. class DmiDeviceRegistry(DeviceRegistry):
  150.     _category_to_property = {
  151.         'BIOS': 'system.firmware',
  152.         'BOARD': 'system.board',
  153.         'CHASSIS': 'system.chassis' }
  154.     
  155.     def __init__(self, properties, category):
  156.         super(DmiDeviceRegistry, self).__init__(properties)
  157.         if category not in self._category_to_property:
  158.             raise Exception, 'Unsupported category: %s' % category
  159.         category not in self._category_to_property
  160.         self._category = category
  161.  
  162.     
  163.     def _property(self):
  164.         return self._category_to_property[self._category]
  165.  
  166.     _property = property(_property)
  167.     
  168.     def _get_category(self):
  169.         return self._category
  170.  
  171.     
  172.     def _get_path(self):
  173.         path = super(DmiDeviceRegistry, self)._get_path()
  174.         return posixpath.join(path, self._category.lower())
  175.  
  176.     
  177.     def _get_product(self):
  178.         for subproperty in ('product', 'type', 'version'):
  179.             property = '%s.%s' % (self._property, subproperty)
  180.             product = self._properties.get(property)
  181.             if product and product != 'Not Available':
  182.                 return product
  183.         
  184.  
  185.     
  186.     def _get_vendor(self):
  187.         for subproperty in ('vendor', 'manufacturer'):
  188.             property = '%s.%s' % (self._property, subproperty)
  189.             if property in self._properties:
  190.                 return self._properties[property]
  191.         
  192.  
  193.     _get_vendor = DmiNotAvailable(_get_vendor)
  194.  
  195.  
  196. class HalRegistry(CommandRegistry):
  197.     '''Registry for HAL information.
  198.  
  199.     Each item contained in this registry consists of the udi as key and
  200.     the corresponding device registry as value.
  201.     '''
  202.     command = String(default = 'lshal')
  203.     _deprecated_expressions = (('info\\.bus', 'info.subsystem'), ('([^\\.]+)\\.physical_device', '\x01.originating_device'), ('power_management\\.can_suspend_to_ram', 'power_management.can_suspend'), ('power_management\\.can_suspend_to_disk', 'power_management.can_hibernate'), ('smbios\\.system\\.manufacturer', 'system.hardware.vendor'), ('smbios\\.system\\.product', 'system.hardware.product'), ('smbios\\.system\\.version', 'system.hardware.version'), ('smbios\\.system\\.serial', 'system.hardware.serial'), ('smbios\\.system\\.uuid', 'system.hardware.uuid'), ('smbios\\.bios\\.vendor', 'system.firmware.vendor'), ('smbios\\.bios\\.version', 'system.firmware.version'), ('smbios\\.bios\\.release_date', 'system.firmware.release_date'), ('smbios\\.chassis\\.manufacturer', 'system.chassis.manufacturer'), ('smbios\\.chassis\\.type', 'system.chassis.type'), ('system\\.vendor', 'system.hardware.vendor'), ('usb_device\\.speed_bcd', 'usb_device.speed'), ('usb_device\\.version_bcd', 'usb_device.version'))
  204.     
  205.     def __init__(self, *args, **kwargs):
  206.         super(HalRegistry, self).__init__(*args, **kwargs)
  207.         self._deprecated_patterns = (lambda .0: for a, b in .0:
  208. (re.compile('^%s$' % a), b))(self._deprecated_expressions)
  209.  
  210.     
  211.     def _get_key(self, key):
  212.         for old, new in self._deprecated_patterns:
  213.             key = old.sub(new, key)
  214.         
  215.         return key
  216.  
  217.     
  218.     def _get_value(self, value, type):
  219.         value = value.strip()
  220.         if type == 'bool':
  221.             value = bool(value == 'true')
  222.         elif type == 'double':
  223.             value = float(value.split()[0])
  224.         elif type == 'int' or type == 'uint64':
  225.             value = int(value.split()[0])
  226.         elif type == 'string':
  227.             value = str(value.strip("'"))
  228.         elif type == 'string list':
  229.             value = [ v.strip("'") for v in value.strip('{}').split(', ') ]
  230.         else:
  231.             raise Exception, 'Unknown type: %s' % type
  232.         return []
  233.  
  234.     
  235.     def _ignore_device(self, device):
  236.         if not device.bus:
  237.             return True
  238.         if not (device.product) and device.product_id is None:
  239.             return True
  240.         if (device.subproduct_id is None or device.subvendor_id is not None or device.subproduct_id is not None) and device.subvendor_id is None:
  241.             return True
  242.         if device.bus != 'dmi' and 'virtual' in device.path.split(posixpath.sep):
  243.             return True
  244.         return False
  245.  
  246.     
  247.     def items(self):
  248.         devices = []
  249.         for record in self.split('\n\n'):
  250.             if not record:
  251.                 continue
  252.             
  253.             name = None
  254.             properties = { }
  255.             for line in record.split('\n'):
  256.                 match = re.match("udi = '(.*)'", line)
  257.                 if match:
  258.                     udi = match.group(1)
  259.                     name = udi.split(posixpath.sep)[-1]
  260.                     continue
  261.                 
  262.                 match = re.match('  (?P<key>.*) = (?P<value>.*) \\((?P<type>.*?)\\)', line)
  263.                 if match:
  264.                     key = self._get_key(match.group('key'))
  265.                     value = self._get_value(match.group('value'), match.group('type'))
  266.                     properties[key] = value
  267.                     continue
  268.             
  269.             if name == 'computer':
  270.                 properties['linux.subsystem'] = 'dmi'
  271.                 properties['linux.sysfs_path'] = '/sys/devices/virtual/dmi/id'
  272.                 device = DeviceRegistry(properties)
  273.                 devices.append(device)
  274.                 for category in ('BIOS', 'BOARD', 'CHASSIS'):
  275.                     device = DmiDeviceRegistry(properties, category)
  276.                     devices.append(device)
  277.                 
  278.             device = DeviceRegistry(properties)
  279.             devices.append(device)
  280.         
  281.         return _[1]
  282.  
  283.     items = cache(items)
  284.  
  285. factory = HalRegistry
  286.